﻿Imports System.Windows.Forms

Public Class frmArchiveScheduled

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        SaveSetting(REGAPP, "ScheduledBackups", "Activated", chkActivate.Checked)
        SaveSetting(REGAPP, "ScheduledBackups", "Folder", txtFolder.Text)
        SaveSetting(REGAPP, "ScheduledBackups", "Time", txtTime.Text)
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub chkActivate_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkActivate.CheckedChanged
        Dim tf As Boolean = chkActivate.Checked
        lblFolder.Enabled = tf
        txtFolder.Enabled = tf
        btnFolder.Enabled = tf
        lblTime.Enabled = tf
        txtTime.Enabled = tf
    End Sub

    Private Sub frmArchiveScheduled_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        chkActivate.Checked = GetSetting(REGAPP, "ScheduledBackups", "Activated", False)
        Dim AppDataPath As String = GetSetting(REGAPP, "General", "AppDataPath", My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\WRDB")
        txtFolder.Text = GetSetting(REGAPP, "ScheduledBackups", "Folder", AppDataPath & "\Archives")
        txtTime.Text = CDate(GetSetting(REGAPP, "ScheduledBackups", "Time", "03:00")).ToString("t")
    End Sub

    Private Sub txtFolder_Validated(sender As Object, e As System.EventArgs) Handles txtFolder.Validated
        txtFolder.Text = txtFolder.Text.TrimEnd(New Char() {"\"})
    End Sub

    Private Sub txtTime_Validated(sender As Object, e As System.EventArgs) Handles txtTime.Validated
        txtTime.Text = CDate(txtTime.Text).ToString("t")
    End Sub

    Private Sub txtTime_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles txtTime.Validating
        If Not IsDate(txtTime.Text) Then e.Cancel = True
    End Sub

    Private Sub btnFolder_Click(sender As System.Object, e As System.EventArgs) Handles btnFolder.Click
        With New FolderBrowserDialog
            .ShowNewFolderButton = True
            .SelectedPath = txtFolder.Text
            If .ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
                txtFolder.Text = .SelectedPath
            End If
        End With
    End Sub

End Class
